package com.auok.Entity;

import Util.JsonUtil;
import Util.SQLUtil;
import io.vertx.core.json.JsonArray;
import io.vertx.reactivex.ext.sql.SQLClient;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

@Getter
@Setter
@AllArgsConstructor
public class UserMessage {
    private String id;
    private String fromUserId;
    private String toUserId;
    private String content;
    private Date time;
    private boolean received;


    /**
     * 持久化消息
     * @param sqlClient
     */
    public void Save(SQLClient sqlClient) {
        String sql = "insert into user_message (time,id,data) values(now(),?,?)";

        var params = new JsonArray()
                .add(this.id)
                .add(JsonUtil.ToJson(this));


        SQLUtil.doInConnection(sqlClient
                , conn -> conn.rxUpdateWithParams(sql, params)
        ).subscribe();
    }
}

/*
 CREATE TABLE user_message
(
	time TIMESTAMPTZ      NOT NULL DEFAULT now(),
	id	 TEXT							PRIMARY key,
	data jsonb						not null
)
 */